package ddb.dbcore;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class DbTable implements Serializable {
    private static final long serialVersionUID = 1L;
    private ArrayList<ArrayList<Object>> rows;

    public DbTable() {
        rows = new ArrayList<ArrayList<Object>>();
    }

    public void initTable(ResultSet rs) throws SQLException {
        while (rs.next()) {
            int numColumns = rs.getMetaData().getColumnCount();
            ArrayList<Object> list = new ArrayList<Object>();
            for (int i = 1; i <= numColumns; i++) {
                // Column numbers start at 1.
                // Also there are many methods on the result set to
                // return
                // the column as a particular type. Refer to the Sun
                // documentation
                // for the list of valid conversions.
                Object o = rs.getObject(i);
                list.add(o);
                // System.out.println("COLUMN " + i + " = " + o.getClass());
            }
            rows.add(list);
        }

    }

    public int getRowCount() {
        return rows.size();
    }

    public ArrayList<Object> rowAt(int index) {
        return rows.get(index);
    }
}
